package hw.ats.news.activity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

import com.humanwares.util.HwUtil;

import hw.ats.common.util.DBHelper;
import hw.ats.news.util.App;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public abstract class DBAwareActivity extends Activity {
	private DBHelper dbHelper = null;
	
	protected String getThumbPath() {
        // 외부확장메모리 확인후 있을 경우 확장메모리에 임시파일 저장하도록 경로 변경
        boolean sdcardExists = HwUtil.externalStorageExists();
        if (sdcardExists) {
        	String path = "/sdcard/"+getApplicationContext().getPackageName()+"/img";
        	File sdcardPath = new File(path);
        	if (!sdcardPath.exists()){
        		sdcardPath.mkdirs();
        		
        		FileOutputStream out;
        		PrintStream p;
        		
        		try {
        			out = new FileOutputStream(path + "/.nomedia");
        			p = new PrintStream( out );
        			p.println("");
        			p.close();
     			
        		}catch(Exception e){
        			Log.d("FILE MAKE","ERROR in FILE MAKE");
        		}
        	}
        	return path ;
        	
        }
        
		return "/data/data/"+getApplicationContext().getPackageName()+"/img";
	}
	
	protected SQLiteDatabase getReadableDatabase() {
		if (dbHelper == null) {
			dbHelper = new DBHelper(this, App.DATABASE_NAME, null, App.DATABASE_VER);
		}
		return dbHelper.getReadableDatabase();
	}

	protected SQLiteDatabase getWritableDatabase() {
		if (dbHelper == null) {
			dbHelper = new DBHelper(this, App.DATABASE_NAME, null, App.DATABASE_VER);
		}
		return dbHelper.getWritableDatabase();
	}

	protected DBHelper getDBHelper() {
		if (dbHelper == null) {
			dbHelper = new DBHelper(this, App.DATABASE_NAME, null, App.DATABASE_VER);
		}
		return dbHelper;
	}
	
	protected void closeDatabase() {
		if (dbHelper != null)
			dbHelper.close();
	}
	
	public DBHelper getOpenDBHelper() {
		if (dbHelper == null) {
			dbHelper = new DBHelper(this, App.DATABASE_NAME, null, App.DATABASE_VER);
		}
		return dbHelper;
	}
}
